<?php

namespace App\Models\Msg;

use App\Models\BaseModel;

class MsgPraise extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'from_id',
        'to_id',
        'post_id',
        'praise_id',
        'status',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'updated_at'
    ];
    protected $table = 'msg_praise';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U';

    /**关联点赞人
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function from_user(){
        return $this->hasOne('App\Models\User\User', 'user_id', 'from_id');
    }

    /**
     * 点赞消息列表
     * @param $to_id
     * @param $request
     * @return mixed
     */
    public function getList($to_id, $request){
        $data = [];
        $data['list'] = $this->withOnly('from_user', ['user_id', 'nickname', 'cover'])
            ->where('to_id', $to_id)
            ->forPage($request['page'], $request['size']+1)
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
        $data['next_page'] = $request['page'] + 1;
        $data['page_size'] = $request['size'];
        return $this->after($data);
    }
}
